
package kaizen.nomweb;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.Properties;

import javax.servlet.*;
import javax.servlet.http.*;

// Referenced classes of package kaizen.nomweb:
//            ejecuta, templates, utilerias

public class pedidolinea extends HttpServlet 
{

  public pedidolinea()
  {
  }

  public static String cacha_parametros(HttpServletRequest httpservletrequest)
  {
    String s = 
      utilerias.validaNulo(httpservletrequest.getParameter("cliente"));
    String s1 = 
      utilerias.validaNulo(httpservletrequest.getParameter("convenio"));
    String s2 = 
      utilerias.validaNulo(httpservletrequest.getParameter("frente"));
    String s3 = 
      utilerias.validaNulo(httpservletrequest.getParameter("referencia"));
    String s4 = 
      utilerias.validaNulo(httpservletrequest.getParameter("comentarios"));
    cadena = "|";
    for (int i = 1; i <= 10; i++)
    {
      String s5 = 
        utilerias.validaNulo(httpservletrequest.getParameter("producto" + i));
      if (!s5.equals("0") && s5 != null)
      {
        String s7 = 
          utilerias.validaNulo(httpservletrequest.getParameter("tonelada" + 
                                                               i));
        String s8 = 
          utilerias.validaNulo(httpservletrequest.getParameter("fecha" + i));
        cadena = cadena + s5 + ";" + s7 + ";" + s8 + "|";
      }
    }

    if (s1.equals(""))
      s1 = "";
    String s6 = 
      s + "," + s1 + "," + s2 + "," + s3 + "," + s4 + ",E," + cadena + "," + 
      usuario + ",";
    return s6;
  }

  public void doGet(HttpServletRequest httpservletrequest, 
                    HttpServletResponse httpservletresponse) throws ServletException, 
                                                                    IOException
  {
    httpservletresponse.setContentType("text/html");
    PrintWriter printwriter = httpservletresponse.getWriter();
    HttpSession httpsession = httpservletrequest.getSession(false);
    if (httpsession != null && httpsession.getAttribute("usuario") != null)
      usuario = httpsession.getAttribute("usuario").toString();
    if (usuario == null)
      usuario = "";
    if (usuario.length() == 0)
      printwriter.println("Error, usuario no valido");
    String s = 
      generaPantalla(httpservletrequest, httpservletresponse, usuario);
    printwriter.println(s);
  }

  public void doPost(HttpServletRequest httpservletrequest, 
                     HttpServletResponse httpservletresponse) throws IOException, 
                                                                     ServletException
  {
    doGet(httpservletrequest, httpservletresponse);
  }

  public static void generaAmbiente(HttpServletRequest httpservletrequest) throws IOException
  {
    String s = utilerias.tomaParametro("dic_sistema", httpservletrequest);
    if (s == null)
      s = "mocte";
    String s1 = "/kaizen/" + s + "Web.properties";
    Properties properties = utilerias.loadProperties(s1);
    dirPlantillasGenerales = properties.getProperty("plantillas");
    datColorRen = "";
    datFont = properties.getProperty("datFont");
    datTamFont = properties.getProperty("datTamFont");
    datColorFont = properties.getProperty("datColorFont");
  }

  public static String generaColumna(String s, String s1, String s2)
  {
    String s3 = "";
    s3 = s3 + "<tr>";
    s3 = 
        s3 + templates.tablaCol(s, datColorFont, datFont, datTamFont, datColorRen);
    s3 = 
        s3 + templates.tablaCol(s1, datColorFont, datFont, datTamFont, datColorRen);
    s3 = 
        s3 + templates.tablaCol(s2, datColorFont, datFont, datTamFont, datColorRen);
    s3 = s3 + "</tr>";
    return s3;
  }

  public static String generaPantalla(HttpServletRequest httpservletrequest, 
                                      HttpServletResponse httpservletresponse, 
                                      String s) throws IOException
  {
    httpservletresponse.setContentType("text/html");
    PrintWriter printwriter = httpservletresponse.getWriter();
    String s1 = "";
    String s2 = "";
    String s3 = "";
    String s4 = "";
    String s5 = utilerias.fechaHoy("dd/MM/yyyy");
    String s6 = "";
    usuario = s;
    generaAmbiente(httpservletrequest);
    String s7 = "SELECT USPE_NORM_REST FROM SCC_USUARIOS_PERFIL ";
    s7 = s7 + " WHERE USPE_CLAVE = UPPER('" + s + "')";
    String s8 = utilerias.valorSqlUni(s7);
    String s9 = "";
    if (s8.equals("N"))
    {
      s9 = 
          " select clnt_numero, clnt_numero ||' - '|| SUBSTR(clnt_razon_social,1,30) from scc_clientes where  clnt_stcl_clave = 'ACTI' order by clnt_numero";
    } else
    {
      s9 = 
          " select clnt_numero, clnt_numero ||' - '|| SUBSTR(clnt_razon_social,1,30) from scc_clientes_usuario, scc_clientes";
      s9 = s9 + " where  clus_uspe_clave = UPPER('" + s + "')";
      s9 = s9 + " and    clnt_numero     = clus_clnt_numero ";
      s9 = s9 + " and    clnt_stcl_clave = 'ACTI'";
      s9 = s9 + " order by 2";
    }
    String s10 = 
      utilerias.validaNulo(httpservletrequest.getParameter("cliente"));
    if (s10.equals("") || s10.equals(null) || s10.equals("null"))
      s10 = "";
    else
      s10 = httpservletrequest.getParameter("cliente");
    s1 = 
        utilerias.replaceString1(templates.generaComboJump("cliente", s9, s10), 
                                 "onChange=\"submit()\"", 
                                 "onChange=\"createCookie('namepedido','" + s + 
                                 "', 1);submit()\"");
    String s11 = "select conv_numero, conv_numero from scc_convenios";
    s11 = s11 + " where  conv_clnt_numero = '" + s10 + "'";
    s11 = s11 + " and    conv_status      = 'A'";
    String s12 = "";
    String s13 = 
      utilerias.validaNulo(httpservletrequest.getParameter("convenio"));
    if (s13.equals("") || s13.equals(null) || s13.equals("null"))
    {
      s13 = "";
      s12 = 
          "SELECT '0' PROD_CLAVE, 'Seleccionar', 1 FROM   DUAL UNION SELECT DISTINCT PROD_CLAVE, substr(PROD_CLAVE || ' - ' ||PROD_DESCRIPCION,1,17) || '('||PRPR_DESCRIPCION||')', 2 FROM   SCC_PRODUCTOS, scc_presentaciones_prod WHERE  PROD_TIPO ='T' AND    PRPR_CLAVE = PROD_PRPR_CLAVE ORDER BY 3";
    } else
    {
      s13 = httpservletrequest.getParameter("convenio");
      s12 = 
          "SELECT '0' PROD_CLAVE, 'Seleccionar', 1 FROM   DUAL UNION SELECT DISTINCT PROD_CLAVE, substr(PROD_CLAVE || ' - ' ||PROD_DESCRIPCION,1,17) || '('||PRPR_DESCRIPCION||')', 2 FROM   SCC_PRODUCTOS, scc_presentaciones_prod, scc_productos_convenio WHERE  PROD_TIPO ='T' AND    PRPR_CLAVE = PROD_PRPR_CLAVE AND    PRCO_PROD_CLAVE  = PROD_CLAVE AND    PRCO_CONV_NUMERO = " + 
          s13 + "AND    PRCO_CLNT_NUMERO = " + s10 + " ORDER BY 3";
    }
    s2 = templates.generaComboJump("convenio", s11, s13);
    String s14 = 
      " SELECT LOCO_CLAVE CLAVE, NVL(LOCO_DESC_CORTA,SUBSTR(LOCA_DESCRIPCION, 1, 20)) DESCRIPCION";
    s14 = s14 + " FROM SCC_LOCALIDADES_CONV, SCC_LOCALIDADES";
    s14 = s14 + " WHERE LOCO_CLNT_NUMERO = '" + s10 + "'";
    s14 = s14 + " AND LOCO_CONV_NUMERO = '" + s13 + "'";
    s14 = s14 + " AND LOCA_CLAVE = LOCO_LOCA_CLAVE";
    s14 = s14 + " UNION";
    s14 = 
        s14 + " SELECT LOCL_CLAVE CLAVE, NVL(LOCL_DESC_CORTA,SUBSTR(LOCA_DESCRIPCION, 20, 12)) DESCRIPCION";
    s14 = s14 + " FROM SCC_LOCALIDADES_CLNT, SCC_LOCALIDADES";
    s14 = s14 + " WHERE LOCL_CLNT_NUMERO =  '" + s10 + "'";
    s14 = s14 + " AND LOCA_CLAVE = LOCL_LOCA_CLAVE";
    s14 = s14 + " AND '" + s13 + "' IS NULL";
    s14 = s14 + " ORDER BY DESCRIPCION";
    String s15 = 
      utilerias.validaNulo(httpservletrequest.getParameter("frente"));
    if (s15.equals("") || s15.equals(null) || s15.equals("null"))
      s15 = "";
    else
      s15 = httpservletrequest.getParameter("frente");
    s3 = templates.generaCombo("frente", s14, s15, false);
    if (httpservletrequest.getParameter("estado") != null && 
        httpservletrequest.getParameter("estado").equals("P"))
    {
      String s16 = 
        utilerias.validaNulo(httpservletrequest.getParameter("cliente"));
      String s18 = 
        utilerias.validaNulo(httpservletrequest.getParameter("convenio"));
      if (s18.equals("") || s18.equals(null))
        s18 = "NULL";
      String s20 = 
        utilerias.validaNulo(httpservletrequest.getParameter("frente"));
      String s22 = cacha_parametros(httpservletrequest);
      ejecuta ejecuta1 = new ejecuta();
      String s25 = 
        ejecuta.store("SCC_P_INSERTA_PEDIDO_INTERNET(,N,N,V,V,V,V,V,V,V)", s22, 
                      8, 1);
      if (s25.indexOf("-") >= 0)
      {
        String s27 = s25.substring(0, s25.indexOf("-"));
        String s29 = s25.substring(s25.indexOf("-") + 1, s25.indexOf(";"));
        btn = 
            "<input type='reset' name='btn_com' value='Comprobante'  OnClick = \"javascript:window.open('/servlet/confirma?serie=" + 
            s27 + "&numero=" + s29 + 
            "','window','resizable,scrollbars=1,width=760,height=400,left = 80,top = 100');\">";
        folio = "Folio Internet: " + s27 + "-" + s29;
      } else
      {
        btn = "";
        folio = s25;
      }
      String s28 = 
        " SELECT SCC_F_VALIDA_CREDITO_CLNT(" + s16 + "," + s18 + ",'" + s20 + 
        "','" + cadena + "') " + " FROM DUAL";
      String s30 = utilerias.valorSqlUni(s28);
      if (!s30.equals("S"))
        error = s30;
      String s32 = 
        " SELECT SCC_F_VALIDA_DOTACIONES_CLNT(" + s16 + ",'" + cadena + "') " + 
        " FROM DUAL";
      String s34 = utilerias.valorSqlUni(s32);
      if (!s34.equals("S"))
        error_dota = s34;
    }
    String s17 = 
      utilerias.validaNulo(httpservletrequest.getParameter("referencia"));
    if (s17.equals("") || s17.equals(null))
      s17 = "";
    else
      s17 = httpservletrequest.getParameter("referencia");
    String s19 = templates.generaTexto("referencia", "10", "10", s17);
    s19 = 
        utilerias.replaceString1(s19, ">", " onChange=\"javascript:this.value=this.value.toUpperCase();\">");
    String s21 = 
      utilerias.validaNulo(httpservletrequest.getParameter("comentarios"));
    if (s21.equals("") || s21.equals(null))
      s21 = "";
    else
      s21 = httpservletrequest.getParameter("comentarios");
    String s23 = 
      templates.generaTextArea("comentarios", "1000", "60", "5", s21);
    s23 = 
        utilerias.replaceString1(s23, ">", " onChange=\"javascript:this.value=this.value.toUpperCase();\">");
    String s24 = 
      "<script language='JavaScript'> \n <!-- \n function valida () \n{ \n   var uno = \"\"     \n   var ss  = \"\";    \n   var s2  = \"\";    \n   var union = \"\";  \n   var dos = \"\";    \n   var s3  = \"\";    \n   var s4  = \"\";    \n   var union2 = \"\"; \nvar bandera = 'N'; \n if (document.data.cliente.value == null || document.data.cliente.value == '') \n { \n   alert('Debe Seleccionar Cliente'); \n   return false; \n } \n if (document.data.frente.value == null || document.data.frente.value == '') \n { \n   alert('Debe Seleccionar Frente de Obra'); \n   return false; \n } \n";
    String s26 = "";
    for (int i = 1; i <= 10; i++)
      s26 = 
          s26 + "if (document.data.producto" + i + ".value != '0') \n" + "{ \n" + 
          "   bandera = 'S'; \n" + "   if (document.data.tonelada" + i + 
          ".value == '0' || document.data.tonelada" + i + ".value == '') \n" + 
          "   { \n" + "   alert('Debe capturar las toneladas'); \n" + 
          "   return false; \n" + "   } \n" + 
          "   var tone = parseInt(\"0\" + document.data.tonelada" + i + 
          ".value); \n" + "   if (isNaN(tone)) \n" + "   {\n" + 
          "      alert('Debe Capturar Solo N\372meros'); \n" + 
          "      return false; \n" + "   } \n" + 
          "   var fecha = document.data.fecha" + i + ".value; \n" + 
          "   var hoy   = \"" + utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
          "   if (Date.parse(fecha) < Date.parse(hoy)) {  \n" + 
          "      alert('La Fecha debe ser Mayor o Igual al dia de Hoy'); \n" + 
          "\t  return false; \n" + "   } \n" + "} \n";

    String s31 = 
      " if (bandera == 'N')  \n{ \nalert ('No ha Capturado Productos') \n return false; \n} \nif (bandera == 'S')  \n{ \n  if(GetCookie(\"namepedido\") == null)\n  {\n    document.write(\"<table width=\\\"100%\\\" height=\\\"100%\\\" border=\\\"0\\\" background=\\\"/scm/media/fondop.jpg\\\">\");\n    document.write(\"  <tr>\");\n    document.write(\"    <td><div align=\\\"center\\\">Su transacci\363n a expirado.<br><a href=\\\"/servlet/servlet/ConectAc?dic_estado=92&dic_menu=m01\\\">Para regresar a <img src=\\\"/scm/media/integra/interior_01.jpg\\\" alt=\\\"enlace@montezuma\\\" width=\\\"187\\\" height=\\\"41\\\" align=\\\"middle\\\"> dar click en la imagen.<a><br></div></td>\");\n    document.write(\"  </tr>\");\n    document.write(\"</table>\");\n    return false;\n  }\n  else\n  {\n    document.data.estado.value = 'P'; \n    document.data.submit(); \n    document.data.Submit.disabled = true; \n    createCookie(\"namepedido\",\"\",-1);\n  } \n} \n} \n\nfunction createCookie(name,value,days)\n{\n   if (days)\n   {\n      if (days > -1)      {         document.data.producto1.value=\"0\";\n         document.data.tonelada1.value=\"0\";\n         document.data.fecha1.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto2.value=\"0\";\n" + 
      "         document.data.tonelada2.value=\"0\";\n" + 
      "         document.data.fecha2.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto3.value=\"0\";\n" + 
      "         document.data.tonelada3.value=\"0\";\n" + 
      "         document.data.fecha3.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto4.value=\"0\";\n" + 
      "         document.data.tonelada4.value=\"0\";\n" + 
      "         document.data.fecha4.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto5.value=\"0\";\n" + 
      "         document.data.tonelada5.value=\"0\";\n" + 
      "         document.data.fecha5.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto6.value=\"0\";\n" + 
      "         document.data.tonelada6.value=\"0\";\n" + 
      "         document.data.fecha6.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto7.value=\"0\";\n" + 
      "         document.data.tonelada7.value=\"0\";\n" + 
      "         document.data.fecha7.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto8.value=\"0\";\n" + 
      "         document.data.tonelada8.value=\"0\";\n" + 
      "         document.data.fecha8.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto9.value=\"0\";\n" + 
      "         document.data.tonelada9.value=\"0\";\n" + 
      "         document.data.fecha9.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.producto10.value=\"0\";\n" + 
      "         document.data.tonelada10.value=\"0\";\n" + 
      "         document.data.fecha10.value=\"" + 
      utilerias.fechaHoy("dd-MM-yyyy") + "\";\n" + 
      "         document.data.comentarios.value=\"\";\n" + "      }" + 
      "      var date = new Date();\n" + 
      "      date.setTime(date.getTime()+(days*24*60*60*1000));\n" + 
      "      var expires = \"; expires=\"+date.toGMTString();\n" + "   }\n" + 
      "   else var expires = \"\";\n" + 
      "   document.cookie = name+\"=\"+value+expires+\";\";\n" + "}\n" + "\n" + 
      "function getCookieVal (offset)\n" + "{\n" + 
      "   var endstr = document.cookie.indexOf (\";\", offset);\n" + 
      "   if (endstr == -1)\n" + "      endstr = document.cookie.length;\n" + 
      "   return unescape(document.cookie.substring(offset, endstr));\n" + 
      "}\n" + "\n" + "function GetCookie (name)\n" + "{\n" + 
      "   var arg = name + \"=\";\n" + "   var alen = arg.length;\n" + 
      "   var clen = document.cookie.length;\n" + "   var i = 0;\n" + 
      "   while (i < clen)\n" + "   {\n" + "      var j = i + alen;\n" + 
      "      if (document.cookie.substring(i, j) == arg)\n" + 
      "         return getCookieVal (j);\n" + 
      "      i = document.cookie.indexOf(\" \", i) + 1;\n" + 
      "      if (i == 0) break;\n" + "   }\n" + "   return null;\n" + "}\n" + 
      "\n //--> \n </script> \n";
    String s33 = "";
    s33 = s24 + s26 + s31;
    String s35 = templates.generaHidden("estado", "");
    s6 = generaTabla(httpservletrequest, s12);
    String s36 = dirPlantillasGenerales + plant_envio;
    String s37 = "";
    String s38 = templates.getHost();
    s37 = templates.generaPlantilla(s36, s38);
    s37 = utilerias.replaceString1(s37, "--txtcliente--", s1);
    s37 = utilerias.replaceString1(s37, "--txtconvenio--", s2);
    s37 = utilerias.replaceString1(s37, "--txtfrenteobra--", s3);
    s37 = utilerias.replaceString1(s37, "--txtfecha--", s5);
    s37 = utilerias.replaceString1(s37, "--txtreferencia--", s19);
    s37 = utilerias.replaceString1(s37, "--txtcomentarios--", s23);
    s37 = utilerias.replaceString1(s37, "--tabla_d--", s6);
    s37 = utilerias.replaceString1(s37, "--hidden2--", s35);
    s37 = utilerias.replaceString1(s37, "--script--", s33);
    s37 = utilerias.replaceString1(s37, "--error--", error);
    s37 = utilerias.replaceString1(s37, "--error_dota--", error_dota);
    s37 = utilerias.replaceString1(s37, "--btn_com--", btn);
    s37 = utilerias.replaceString1(s37, "--folio--", folio);
    s37 = 
        utilerias.replaceString1(s37, "value=\"Procesar\">", httpservletrequest.getParameter("estado") == 
                                                             null || 
                                                             !httpservletrequest.getParameter("estado").equals("P") ? 
                                                             "value=\"Procesar\">" : 
                                                             "value=\"Procesar\" disabled>");
    folio = "";
    btn = "";
    error = "";
    error_dota = "";
    return s37;
  }

  public static String generaTabla(HttpServletRequest httpservletrequest, 
                                   String s) throws IOException
  {
    String s1 = "";
    String s2 = utilerias.fechaHoy("dd-MM-yyyy");
    String s3 = "";
    String s5 = "";
    String s7 = "";
    String s9 = "";
    String s10 = "";
    String s11 = "";
    String s12 = "";
    for (int i = 1; i <= 10; i++)
    {
      String s13 = 
        utilerias.validaNulo(httpservletrequest.getParameter("producto" + i));
      if (s13.equals("") || s13.equals(null))
        s13 = "";
      else
        s13 = httpservletrequest.getParameter("producto" + i);
      String s4 = templates.generaCombo("producto" + i, s, s13);
      String s14 = 
        utilerias.validaNulo(httpservletrequest.getParameter("tonelada" + i));
      if (s14.equals("") || s14.equals(null))
        s14 = "0";
      else
        s14 = httpservletrequest.getParameter("tonelada" + i);
      String s6 = templates.generaTexto("tonelada" + i, "8", "8", s14);
      String s15 = 
        utilerias.validaNulo(httpservletrequest.getParameter("fecha" + i));
      if (s15.equals("") || s15.equals(null))
        s15 = utilerias.fechaHoy("dd-MM-yyyy");
      else
        s15 = httpservletrequest.getParameter("fecha" + i);
      String s8 = templates.generaCalendario("fecha" + i, s15);
      s1 = s1 + generaColumna(s4, s6, s8);
    }

    return s1;
  }

  static String dirPlantillasGenerales = "";
  static String datColorRen = "";
  static String datFont = "";
  static String datTamFont = "";
  static String datColorFont = "";
  static String plant_envio = "pgral_ideograma/sol_envio.html";
  static String error = "";
  static String error_dota = "";
  static String cadena = "|";
  static String estado = "O";
  static String btn = "";
  static String folio = "";
  static String usuario = "";

}
